package ext.tianma.part;

import java.util.Hashtable;

import org.json.JSONException;
import org.json.JSONObject;

import ext.com.core.CoreUtil;
import ext.com.iba.IBAUtil;
import wt.part.WTPart;
import wt.session.SessionServerHelper;
import wt.util.WTException;
/**
 * 将成品的IBA属性同步到plm
 * @author deng
 *
 */
public class PLMSynchronousPartService {
	
	public static String synchronousPartMessage(String productNumber, String view, String projectNumber, String projectName, String projectManager) throws JSONException, WTException {
		
		JSONObject ob = new JSONObject();
		WTPart part = CoreUtil.getWTPartByNumberAndView(productNumber,view);//根据成品编号和视图获得物料
		boolean flagAccess = SessionServerHelper.manager.setAccessEnforced(false);//忽略权限
		try {
			if(part != null) {
				Hashtable updateIBAValues = new Hashtable();
				updateIBAValues.put("cn.tianma.projectNumber", projectNumber);//修改项目编号
				updateIBAValues.put("cn.tianma.projectName", projectName);//修改项目名称
				updateIBAValues.put("cn.tianma.projectManager", projectManager);//修改项目经理
				part = (WTPart) CoreUtil.checkoutObject(part);
				IBAUtil.forceSetIBAValues(part, updateIBAValues);
				ob.put("status", "success");
				ob.put("message", "同步成功!");
			}else {
				ob.put("status", "false");
				ob.put("message", "没找到成品!");
			}
		} catch (Exception e) {
			ob.put("status", "error");
			ob.put("message", "接口错误！");
			e.printStackTrace();
		} finally {
			// 检入零部件
			CoreUtil.checkinObject(part, "修改属性成功");
			SessionServerHelper.manager.setAccessEnforced(flagAccess);
	}
		return ob.toString();
	}
/**
 * 清空成品项目编号，项目名称，项目经理IBA属性
 * 
 * @param productNumber 成品编号
 * @param view 视图
 * @return
 * @throws JSONException
 * @throws WTException 
 */
	public static String emptyPartIBA(String productNumber, String view) throws JSONException, WTException {
		JSONObject ob = new JSONObject();
		WTPart part = CoreUtil.getWTPartByNumberAndView(productNumber,view);//根据成品编号和视图获得物料
		boolean flagAccess = SessionServerHelper.manager.setAccessEnforced(false);//忽略权限
		try {
			if(part != null) {
				Hashtable updateIBAValues = new Hashtable();
				updateIBAValues.put("cn.tianma.projectNumber", "");//修改项目编号
				updateIBAValues.put("cn.tianma.projectName", "");//修改项目名称
				updateIBAValues.put("cn.tianma.projectManager", "");//修改项目经理
				part = (WTPart) CoreUtil.checkoutObject(part);
				IBAUtil.forceSetIBAValues(part, updateIBAValues);
				ob.put("status", "success");
				ob.put("message", "同步成功!");
			}else {
				ob.put("status", "false");
				ob.put("message", "没找到成品!");
			}
		} catch (Exception e) {
			ob.put("status", "error");
			ob.put("message", "接口错误！");
			e.printStackTrace();
		} finally {
			// 检入零部件
			CoreUtil.checkinObject(part, "修改属性成功");
			SessionServerHelper.manager.setAccessEnforced(flagAccess);
	}
		return ob.toString();
	}

}
